<template>
  <grid title="项目管理">
    <el-form inline
             style="width: 100%; text-align: left"
             label-width="60px"
             @submit.native.prevent="onSubmit"
             @reset.native.prevent="onReset"
             size="small">
      <el-form-item label="链接">
        <el-input v-model="query.q"
                  placeholder="请输入关键词"
                  clearable></el-input>
      </el-form-item>

      <el-form-item label="发布状态"
                    label-width="80px">
        <el-select v-model="query.status">
          <el-option label="全部"
                     value="all"></el-option>
          <el-option label="未发布"
                     value="down"></el-option>
          <el-option label="发布中"
                     value="up"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   native-type="submit">筛选</el-button>
        <el-button native-type="reset">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right">
        <router-link to="/project/add">
          <el-button type="primary">新建项目</el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <el-table :data="list"
              border
              :resizable="false"
              stripe>
      <el-table-column type="index"
                       label="编号"
                       width="60"
                       align="center"></el-table-column>
      <el-table-column prop="image.url"
                       label="图片">
        <template slot-scope="scope">
          <el-image style="max-height: 150px;"
                    :src="scope.row.image.url"
                    fit="contain"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="title"
                       label="标题">
      </el-table-column>
      <el-table-column prop="summary"
                       label="简介">
      </el-table-column>
      <el-table-column prop="website"
                       label="链接"
                       show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary"
                   :href="scope.row.website"
                   target="_blank">{{scope.row.website}}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="status"
                       label="状态">
        <template slot-scope="scope">
          <normal-status :data="scope.row"></normal-status>
        </template>
      </el-table-column>
      <el-table-column prop="created_at"
                       label="创建时间">
        <template slot-scope="scope">
          {{scope.row.created_at | timeFilter }}
        </template>
      </el-table-column>
      <el-table-column fixed="right"
                       label="操作"
                       width="240">
        <template slot-scope="scope">
          <div class="operation_body">
            <router-link :to="{ name: 'project/edit', params: { id: scope.row.id.toString() } }"
                         style="margin-right: 10px">
              <el-button type="text"
                         size="small">修改</el-button>
            </router-link>
            <template v-if="scope.row.status === 1">
              <el-button type="text"
                         @click="offlineRow(scope.row.id)"
                         size="small">下架</el-button>
            </template>
            <template v-else>
              <el-button type="text"
                         @click="onlineRow(scope.row.id)"
                         size="small">上架</el-button>
            </template>
            <el-button type="text"
                       @click="deleteRow(scope.row.id)"
                       size="small">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="total, prev, pager, next"
                   background
                   @current-change="fetch"
                   :current-page.sync="query.page"
                   :page-size="query.limit"
                   hide-on-single-page
                   style="display: flex;justify-content: center;"
                   :total="total">
    </el-pagination>

  </grid>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import listMixin from '@/mixin/list'
import imageInput from '@/components/image-input'
import { fetchView, store, edit, destory, offline, online } from './api'

export default {
  title: '项目管理',
  data () {
    return {
      query: {
        limit: 12,
        page: 1,
        q: undefined,
        status: 'all'
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空' }
        ]
      }
    }
  },
  mixins: [listMixin],
  components: {
    imageInput
  },
  async asyncData ({ store, route }) {
    const { query } = route;
    return store.dispatch('project/fetchList', { page: 1, limit: 12 })
  },
  computed: {
    ...mapGetters('project', ['list', 'total'])
  },
  methods: {
    ...mapActions('project', ['fetchList']),
    fetch () {
      this.fetchList(this.query)
    },
    async offlineRow (id) {
      await this.$confirm('此操作将隐藏该项目 确定执行？', '警告')
      await offline(id)
      this.$message.success('操作成功')
      this.fetchList(this.query)
    },
    async onlineRow (id) {
      await this.$confirm('此操作将显示该项目 确定执行？', '警告')
      await online(id)
      this.$message.success('操作成功')
      this.fetchList(this.query)
    },
    async deleteRow (id) {
      await this.$confirm('确定删除？')
      await destory(id)
      this.$message.success('删除成功')
      this.fetchList(this.query)
    }
  }
}
</script>
